import java.util.ArrayList;
import java.util.Comparator;

public class PersonAgeSort {
    public static void main(String[] args) {
        Person person1=new Person("zhangsan",20,"financer");
        Person person2=new Person("lisi",40,"manager");
        Person person3=new Person("bruce",35,"shipper");
        int max=0;
        int min=person1.getAge();
        int middle=0;
        ArrayList<Person> arrayList=new ArrayList<>();
        arrayList.add(person1);
        arrayList.add(person2);
        arrayList.add(person3);
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getAge()>max){
                max=arrayList.get(i).getAge();

            }
        }
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getAge() <min){
                min=arrayList.get(i).getAge();

            }
        }
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i).getAge()!=max && arrayList.get(i).getAge()!=min){
                middle=arrayList.get(i).getAge();
            }
        }

        System.out.println("年龄的最大值是："+max);
        System.out.println("年龄的中间值是:"+middle);
        System.out.println("年龄的最小值是："+min);

        arrayList.sort(Comparator.comparing(Person::getAge));
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i).getAge());
        }
        Person[] persons=new Person[3];
        persons[0]=person1;
        persons[1]=person2;
        persons[2]=person3;
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);
        }
        //使用冒泡排序
        Person tmp=null;
        for (int i = 0; i <persons.length-1 ; i++) {
            for (int j = 0; j < persons.length-1-i; j++) {
                //年龄从大到小排序，如果起那么的人的年龄<后面人的年龄，就交换
                if(persons[i].getAge()<persons[i+1].getAge()){
                    tmp=persons[i];
                    persons[i]=persons[i+1];
                    persons[i+1]=tmp;
                }

            }

        }
//        打印冒泡排序后的年龄降序排序效果
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);
        }
        System.out.println("-------------");
        Person tmp2=null;
        //按照年龄从小到大排序
        for (int i = 0; i < persons.length; i++) {
            for (int j = 0; j < persons.length-1-i; j++) {
                if(persons[j].getAge()>persons[j+1].getAge()){
                    tmp2=persons[j];
                    persons[j]=persons[j+1];
                    persons[j+1]=tmp2;
                }

            }

        }

        for (int i = 0; i <persons.length ; i++) {
            System.out.println(persons[i]);
        }
    }

}
